package com.yuyou.fn.platform.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.platform.entity.AreaTeam;
import com.yuyou.fn.platform.entity.BusTeam;
import com.yuyou.fn.platform.service.IAreaTeamBusTeamOrgService;
import com.yuyou.fn.platform.service.IAreaTeamService;
import com.yuyou.fn.platform.service.IBusTeamService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by wens on 2017/7/21.
 */
@Service
public class AreaTeamBusTeamOrgServiceImpl implements IAreaTeamBusTeamOrgService {

    @Resource
    private IAreaTeamService areaTeamService ;

    @Resource
    private IBusTeamService busTeamService ;

    @Override
    public JSONArray findOrgTree() {
        List<AreaTeam> areaTeamList = areaTeamService.findAllAreaTeam();
        List<Long> areaTeamIdList = BeanUtils.toList(areaTeamList,"areaTeamId");
        List<BusTeam> busTeamList = busTeamService.findBusTeamByAreaTeamIds(areaTeamIdList);

        Map<Long,List<BusTeam>> busTeamGroup =  BeanUtils.toGroup(busTeamList,"areaTeamId") ;
        JSONArray ret  = new JSONArray();
        //暂时不考虑权限传递
        for(AreaTeam areaTeam : areaTeamList ){

            JSONObject o1 = new JSONObject();
            o1.put("areaTeamId" , areaTeam.getAreaTeamId() );
            o1.put("name" , areaTeam.getName());

            List<BusTeam> busTeamList1 = busTeamGroup.get(areaTeam.getAreaTeamId());
            if(busTeamList1 != null && !busTeamList1.isEmpty() ){
                JSONArray ret2  = new JSONArray();

                for(BusTeam busTeam : busTeamList1 ){
                    JSONObject o2 = new JSONObject();
                    o2.put("busTeamId",busTeam.getBusTeamId());
                    o2.put("name" , busTeam.getName() );
                    ret2.add(o2);
                }

                o1.put("busTeamList" , ret2 );
            }
            ret.add(o1) ;
        }

        return ret ;
    }
}
